<template>
  <div class="login">
    <!-- 登录页头部 -->
    <!-- <div class="login-header">
      <div class="flex-y-center">
        <el-switch
          v-model="isDark"
          inline-prompt
          active-icon="Moon"
          inactive-icon="Sunny"
          @change="toggleTheme"
        />
        <lang-select class="ml-2 cursor-pointer" />
      </div>
    </div> -->

    <el-row class="login-row">
      <el-col :span="12" :push="12" class="form-col">
        <!-- 登录页内容 -->
        <div class="form-title">
          <h1 class="welcome">欢迎登陆系统!</h1>
          <h2 class="main-title">四川省发电企业数据分析系统</h2>
        </div>
        <div class="login-form">
          <h4>账号密码登录</h4>
          <el-form ref="loginFormRef" :model="loginFormData" :rules="loginRules">
            <!-- 用户名 -->
            <el-form-item prop="username">
              <div class="input-wrapper">
                <el-icon class="mx-2">
                  <User />
                </el-icon>
                <el-input
                  ref="username"
                  v-model="loginFormData.username"
                  :placeholder="$t('login.username')"
                  name="username"
                  size="large"
                  class="h-[48px]"
                />
              </div>
            </el-form-item>

            <!-- 密码 -->
            <el-tooltip :visible="isCapslock" :content="$t('login.capsLock')" placement="right">
              <el-form-item prop="password">
                <div class="input-wrapper">
                  <el-icon class="mx-2">
                    <Lock />
                  </el-icon>
                  <el-input
                    v-model="loginFormData.password"
                    :placeholder="$t('login.password')"
                    type="password"
                    name="password"
                    size="large"
                    class="h-[48px] pr-2"
                    show-password
                    @keyup="checkCapslock"
                    @keyup.enter="handleLoginSubmit"
                  />
                </div>
              </el-form-item>
            </el-tooltip>

            <!-- 验证码 -->
            <el-form-item prop="code" v-if="captchaEnable">
              <div class="input-wrapper">
                <div class="i-svg:captcha mx-2" />

                <el-input
                  v-model="loginFormData.code"
                  auto-complete="off"
                  size="large"
                  class="flex-1"
                  :placeholder="$t('login.captchaCode')"
                  @keyup.enter="handleLoginSubmit"
                />

                <el-image :src="captchaBase64" class="captcha-img" @click="getCaptcha" />
              </div>
            </el-form-item>

            <!-- <div class="flex-x-between w-full py-1">
              <el-checkbox>
                {{ $t('login.rememberMe') }}
              </el-checkbox>

              <el-link type="primary" href="/forget-password">
                {{ $t('login.forgetPassword') }}
              </el-link>
            </div> -->

            <!-- 登录按钮 -->
            <el-button
              :loading="loading"
              type="primary"
              size="large"
              class="w-full"
              @click.prevent="handleLoginSubmit"
            >
              {{ $t('login.login') }}
            </el-button>

            <!-- 第三方登录 -->
            <!-- <el-divider>
              <el-text size="small">{{ $t('login.otherLoginMethods') }}</el-text>
            </el-divider>
            <div class="third-party-login">
              <div class="i-svg:wechat" />
              <div class="i-svg:qq" />
              <div class="i-svg:github" />
              <div class="i-svg:gitee" />
            </div> -->
          </el-form>
        </div>
      </el-col>
    </el-row>

    <!-- 登录页底部 -->
    <!-- <div class="login-footer">
      <el-text size="small">
        Copyright © 2021 - 2025 youlai.tech All Rights Reserved.
        <a href="http://beian.miit.gov.cn/" target="_blank">皖ICP备20006496号-2</a>
      </el-text>
    </div> -->
  </div>
</template>

<script setup lang="ts">
import { LocationQuery, RouteLocationRaw, useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';

import { type LoginFormData } from '@/api/auth.api';
import router from '@/router';

import type { FormInstance } from 'element-plus';

// import defaultSettings from '@/settings';
import { ThemeMode } from '@/enums/settings/theme.enum';

import { useSettingsStore, useUserStore } from '@/store';
import { generateUUID, adaptationUrl } from '@/utils/crypto';

const userStore = useUserStore();
const settingsStore = useSettingsStore();

const route = useRoute();
const { t } = useI18n();
const loginFormRef = ref<FormInstance>();

const isDark = ref(settingsStore.theme === ThemeMode.DARK); // 是否暗黑模式
const loading = ref(false); // 按钮 loading 状态
const isCapslock = ref(false); // 是否大写锁定
const captchaBase64 = ref(); // 验证码图片Base64字符串

const loginFormData = ref<LoginFormData>({
  username: '',
  password: null,
  // captchaKey: '',
  code: '', // 验证码
  randomStr: '', // 验证码随机数
});
const captchaEnable = ref(import.meta.env.VITE_VERIFY_ENABLE === 'true');

const loginRules = computed(() => {
  return {
    username: [
      {
        required: true,
        trigger: 'blur',
        message: t('login.message.username.required'),
      },
    ],
    password: [
      {
        required: true,
        trigger: 'blur',
        message: t('login.message.password.required'),
      },
      {
        min: 6,
        message: t('login.message.password.min'),
        trigger: 'blur',
      },
    ],
    code: [
      {
        required: true,
        trigger: 'blur',
        message: t('login.message.captchaCode.required'),
      },
    ],
  };
});

// 获取验证码
function getCaptcha() {
  const str = generateUUID();
  loginFormData.value.randomStr = str;
  const url = `${import.meta.env.VITE_API_URL}/code/image`;
  // captchaBase64.value = `${import.meta.env.VITE_API_URL}${import.meta.env.VITE_IS_MICRO == 'false' ? '/admin' : '/auth'}/code/image?randomStr=${str}`;
  captchaBase64.value = `${import.meta.env.VITE_APP_BASE_API}${adaptationUrl(url)}?randomStr=${str}`;
}

// 登录提交处理
async function handleLoginSubmit() {
  try {
    // 1. 表单验证
    const valid = await loginFormRef.value?.validate();
    if (!valid) return;

    loading.value = true;

    // 2. 执行登录
    await userStore.login(loginFormData.value);

    // 3. 获取用户信息
    await userStore.getUserInfo();
    ElMessage.success('登录成功');

    // 4. 登录成功，让路由守卫处理跳转逻辑
    // 解析目标地址，但不直接跳转
    const redirect = resolveRedirectTarget(route.query);
    console.log("🎉 Login successful, target redirect:", redirect);

    setTimeout(async () => {
      // 通过替换当前路由触发路由守卫，让守卫处理后续的路由生成和跳转
      await router.replace(redirect);
    }, 10);

    // 5. 记住我功能已实现，根据用户选择决定token的存储方式:
    // - 选中"记住我": token存储在localStorage中，浏览器关闭后仍然有效
    // - 未选中"记住我": token存储在sessionStorage中，浏览器关闭后失效
  } catch (error) {
    // 6. 统一错误处理
    getCaptcha(); // 刷新验证码
    console.error('登录失败:', error);
  } finally {
    loading.value = false;
  }
}

/**
 * 解析重定向目标
 *
 * @param query 路由查询参数
 * @returns 标准化后的路由地址
 */
function resolveRedirectTarget(query: LocationQuery): RouteLocationRaw {
  // 默认跳转路径
  const defaultPath = '/';

  // 获取原始重定向路径
  const rawRedirect = (query.redirect as string) || defaultPath;

  try {
    // 6. 使用Vue Router解析路径
    const resolved = router.resolve(rawRedirect);
    return {
      path: resolved.path,
      query: resolved.query,
    };
  } catch(error) {
    console.log('解析重定向目标失败:', error);
    // 7. 异常处理：返回安全路径
    return { path: defaultPath };
  }
}

// 主题切换
const toggleTheme = () => {
  const newTheme = settingsStore.theme === ThemeMode.DARK ? ThemeMode.LIGHT : ThemeMode.DARK;
  settingsStore.changeTheme(newTheme);
};

// 检查输入大小写
function checkCapslock(event: KeyboardEvent) {
  // 防止浏览器密码自动填充时报错
  if (event instanceof KeyboardEvent) {
    isCapslock.value = event.getModifierState('CapsLock');
  }
}

// 设置登录凭证
const setLoginCredentials = (username: string, password: string) => {
  loginFormData.value.username = username;
  loginFormData.value.password = password;
};

onMounted(() => {
  getCaptcha();
});
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  /* padding: 20px; */
  overflow-y: auto;
  background: url('@/assets/images/loginBg.png') no-repeat center;
  background-size: auto 100%;
  background-position-x: left;
  /**
    background-repeat: repeat-y;
    背景平铺方式：
    repeat：即默认方式，完全平铺背景
    no-repeat：在X及Y轴方向均不平铺
    repeat-x：横向平铺背景
    repeat-y：纵向平铺背景 
  */

  .login-header {
    position: absolute;
    top: 0;
    display: flex;
    justify-content: right;
    width: 100%;
    padding: 15px;

    .title {
      margin: auto 5px;
      font-size: 24px;
      font-weight: bold;
      color: #3b82f6;
    }
  }

  .login-row {
    width: 100%;
    height: 100%;
  }

  .form-col {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  .welcome {
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 36px;
    color: #021f0e;
  }
  .main-title {
    font-family: DingTalk JinBuTi, DingTalk JinBuTi;
    font-weight: 600;
    font-size: 28px;
    color: #1d2129;
    // font-style: italic;
    // transform: skew(-10deg, 0deg);
    letter-spacing: 5px;
  }

  .logo-title {
    font-family: Microsoft Yahei;
    font-weight: 600;
    font-size: 22px;
    padding: 50px 0 0;
    text-align: center;
  }

  .login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 450px;
    // height: 680px;
    padding: 40px;
    overflow: hidden;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    margin: 0 auto;
    // box-shadow: var(--el-box-shadow-light);

    @media (width <= 460px) {
      width: 100%;
      padding: 20px;
    }

    .form-title {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 0 20px;
      text-align: center;
    }

    .input-wrapper {
      display: flex;
      align-items: center;
      width: 100%;
    }

    .mode {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .captcha-img {
      height: 48px;
      cursor: pointer;
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;
    }

    .third-party-login {
      display: flex;
      justify-content: center;
      width: 100%;
      color: var(--el-text-color-secondary);

      *:not(:first-child) {
        margin-left: 20px;
      }

      .icon {
        cursor: pointer;
      }
    }
  }

  .inform {
    background-color: #fff;
  }
  .login-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 10px 0;
    text-align: center;
  }
}

:deep(.el-form-item) {
  // background: var(--el-input-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 5px;
}

:deep(.el-input) {
  .el-input__wrapper {
    padding: 0;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      /* 通过延时渲染背景色变相去除背景颜色 */
      transition: background-color 1000s ease-in-out 0s;
    }
  }
}

html.dark {
  .login {
    background: url('@/assets/images/login-bg-dark.jpg') no-repeat center right;

    .login-form {
      background: transparent;
      box-shadow: var(--el-box-shadow);
    }
  }
}
</style>
